package com.weilai.lzt.sms3;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static android.content.Context.MODE_WORLD_WRITEABLE;

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage msg = null;
        SharedPreferences read = context.getSharedPreferences("openid",MODE_WORLD_WRITEABLE);
        String openid = read.getString("openid", "");
        if (null != bundle) {
            Object[] smsObj = (Object[]) bundle.get("pdus");
            for (Object object : smsObj) {
                msg = SmsMessage.createFromPdu((byte[]) object);
                Date date = new Date(msg.getTimestampMillis());//时间
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String receiveTime = format.format(date);
                Log.i("lzt", "number:" + msg.getOriginatingAddress()+ "   body:" + msg.getDisplayMessageBody() + "  time:"+ msg.getTimestampMillis());
                Map<String,String> data = new HashMap();
                data.put("address", msg.getOriginatingAddress());
                data.put("person", "");
                data.put("date", receiveTime);
                data.put("body", msg.getDisplayMessageBody());
                data.put("touser", openid);
                data.put("jwid", "gh_addf7cdb6161");
                data.put("template_id", "D9XSkarjqeZPUqc2WVUXn6GarvosSC6a6qyp7bNsmFc");
                new MyThread(data).start();
            }
        }
    }

    /**
     * 异步线程发送请求
     * @author Administrator
     *
     */
    class MyThread extends Thread{
        private String url ="http://www.online188.cn/jeewx/wechatController/sendsms.do";
        private Map<String,String> data;
        public MyThread(Map<String,String> data){
            this.data = data;
        }
        public void run(){
            sendPost(url,data);
        }
    }


    private void sendPost(String http_url,Map<String,String> data){
        Log.i("lzt", data.toString());
        try {
            URL url = new URL(http_url);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(10 * 1000);
            String body = "";
            for (Map.Entry<String, String> entry : data.entrySet()) {
                body += entry.getKey()+"="+entry.getValue()+"&";
            }
            conn.setRequestProperty("Cache-Control", "max-age=0");
            //设置conn可以写请求的内容
            conn.setDoOutput(true);
            conn.getOutputStream().write(body.getBytes());
            conn.connect();
            BufferedReader bufReader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"GBK"));
            Log.i("lzt", bufReader.toString());
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("lzt", e.getMessage());
        }

    }


}
